package top.haichi.webpaper.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import top.haichi.webpaper.entity.Notice;
import top.haichi.webpaper.entity.Paper;
import top.haichi.webpaper.entity.Result;
import top.haichi.webpaper.mapper.NoticeMapper;
import top.haichi.webpaper.mapper.PaperMapper;
import top.haichi.webpaper.mapper.StudentMapper;

import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

@RestController
@Slf4j
public class AdminController {
    @Autowired
    private PaperMapper paperMapper;
    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 管理员搜索论文
     * @param jsonNode
     * @return
     */
    @PostMapping("/admin/getpapers")
    public Result getPapers(@RequestBody JsonNode jsonNode) {
        String keyWords = jsonNode.get("keyWords").asText();
        QueryWrapper<Paper> wrapper = new QueryWrapper<>();
        wrapper.like("title",keyWords);
        List<Paper> papers = paperMapper.selectList(wrapper);
        return Result.success("获取成功",papers);
    }

    /**
     * 管理员修改论文状态
     * @param jsonNode
     * @return
     */
    @PostMapping("/admin/setstatus")
    public Result setStatus(@RequestBody JsonNode jsonNode){
        String paperId = jsonNode.get("paperId").asText();
        String status = jsonNode.get("status").asText();
        if(!(status.equals("审核中")||status.equals("待修改")||status.equals("已完成"))){
            return Result.error("你干嘛");
        }else{
            //没校验论文是不是自己学生的qaq
            Paper selectedPaper = paperMapper.selectById(paperId);
            selectedPaper.setStatus(status);
            paperMapper.updateById(selectedPaper);
            return Result.success("修改成功!");
        }
    }

    /**
     * 管理员发送公告
     * @param notice
     * @return
     */
    @PostMapping("/admin/sendnotice")
    public Result sendNotice(@RequestBody Notice notice){
        try {
            notice.setCreated(LocalDate.now());
            notice.setId(UUID.randomUUID().toString());
            noticeMapper.insert(notice);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("发送失败,呜呜呜");
        }
        return Result.success("发送成功");
    }
}
